package de.tu_darmstadt.gdi1.xcom.controller.ai;

import java.util.List;

import de.tu_darmstadt.gdi1.framework.utils.Point;
import de.tu_darmstadt.gdi1.xcom.controller.XcomController;
import de.tu_darmstadt.gdi1.xcom.controller.events.ActionEnum;
import de.tu_darmstadt.gdi1.xcom.controller.events.UserActionEnum;
import de.tu_darmstadt.gdi1.xcom.controller.events.XcomActionEvent;
import de.tu_darmstadt.gdi1.xcom.controller.events.XcomUserActionEvent;
import de.tu_darmstadt.gdi1.xcom.controller.util.XcomLevelParser;
import de.tu_darmstadt.gdi1.xcom.model.elements.Mob;

public class XcomSimpleAI implements XcomAIInterface {

	private XcomLevelParser parser;
	private XcomAIGame aiGame;
	
	private String makeTurn(){
		double test = Math.random();
		List<Mob> mobs = aiGame.getActiveTeamMobs();
		Mob currentMob = selectMob(mobs);
		ActionEnum ae = null;
		if(test < 0.5 && currentMob.getPoints() > 3){
			ae = ActionEnum.Move;
		}if(test >= 0.5 && test < 0.75 && currentMob.getPoints() > 1){
			ae = ActionEnum.RotateLeft;
		}if(test >= 0.75 && test < 1.0 && currentMob.getPoints() > 1){
			ae = ActionEnum.RotateRight;
		}
		if(ae == null)
			ae = ActionEnum.EndTurn;
		Point pos = currentMob.getPosition();
		return "" + pos.getX() + ";" + pos.getY() + ";" + ae.getChar();
	}
	private Mob selectMob(List<Mob> mobs) {
		double test = Math.random();
		int index = (int) Math.round(test * (mobs.size() - 1));
		return mobs.get(index);
	}

	@Override
	public String getMove(String game) {
		if(parser == null)
			parser = new XcomLevelParser(game);
		else
			parser.setLevel(game);
		
		aiGame = parser.parse();
		
		return makeTurn();
	}
}
